﻿using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
using Text.IDal;

namespace Text.Dal
{
    public class BaseDal<T> : IBaseDal<T> where T : class
    {
        TextDbContext db;

        public BaseDal(TextDbContext db)
        {
            this.db = db;
        }



        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t">某个实体</param>
        /// <returns>受影响行数</returns>
        public async Task<int> ADD(T t)
        {
            await db.Set<T>().AddAsync(t);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 显示列表
        /// </summary>
        /// <param name="exp">查询条件</param>
        /// <returns>返回显示的数据</returns>
        public IQueryable<T> GetList()
        {
            return db.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns>返回查询的数据</returns>
        public async Task<T> GetValue(Expression<Func<T, bool>> exp)
        {
           return await db.Set<T>().Where(exp).FirstOrDefaultAsync();
        }

    }
}
